package com.googlecode.horncomb.orm.ibatis;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;

import com.googlecode.horncomb.bean.filter.ConditionSet;
import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.bean.paged.CollectionWarpper;
import com.googlecode.horncomb.bean.paged.PagedList;

/**
 * SqlMap entity DAO implement. <br/>
 * @author Daniel
 */
@SuppressWarnings("unchecked")
public class SqlMapEntityDaoImpl<EntityType> extends
		AbstractCommonSqlMapEntityDao implements SqlMapEntityDao<EntityType> {
	private static final long serialVersionUID = 2383392591630044311L;

	/**
	 * default constructor
	 */
	public SqlMapEntityDaoImpl() {
		super();
	}

	// ----------------------------------------------------------------------
	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#getById(java.io.Serializable)
	 * @see #getById(Serializable, String)
	 */
	public EntityType getById(Serializable id) throws DataAccessException {
		return (EntityType) this.getById(id, this.getStatementName);
	}

	@Override
	protected EntityType doGet(ConditionSet cons) throws DataAccessException {
		return (EntityType) super.doGet(cons);
	}

	@Override
	protected EntityType doGet(ConditionSet cons, String stmtName)
			throws DataAccessException {
		return (EntityType) super.doGet(cons, stmtName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#insert(java.io.Serializable)
	 * @see #insert(Serializable, String)
	 */
	public int insert(EntityType entity) throws DataAccessException {
		return this.insert(entity, this.insertStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see cn.horncomb.orm.dao.EntityBaseDao#update(java.io.Serializable)
	 */
	public int update(EntityType entity) throws DataAccessException {
		return this.update(entity, this.updateStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#deleteById(java.io.Serializable)
	 * @see #deleteById(Serializable, String)
	 */
	public int deleteById(Serializable id) throws DataAccessException {
		return this.deleteById(id, this.deleteStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.dao.EntityBaseDao#deleteByIds(Set)
	 * @see #deleteByIds(Set, String);
	 */
	public int deleteByIds(Set ids) throws DataAccessException {
		return this.deleteByIdSet(ids, this.deleteStatementName);
	}

	/**
	 * {@inheritDoc}
	 * @see cn.horncomb.orm.dao.EntityBaseDao#save(java.io.Serializable)
	 */
	public int save(EntityType entity) throws DataAccessException {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("save entity: " + entity);
		}
		if (this.isNew(entity)) {
			return this.insert(entity);
		} else {
			return this.update(entity);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryByExample(java.io.Serializable,
	 *      java.util.Collection, int, int)
	 */
	public PagedList<List<? extends EntityType>> queryByExample(
			EntityType example, Order order, int page, int pageSize) {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example), order, page, pageSize);
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryCountByExample(java.io.Serializable)
	 */
	public int queryCountByExample(EntityType example) {
		return this.queryCount(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example));
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection<EntityType> queryByExample(EntityType example) {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example));
	}

	/**
	 * {@inheritDoc}
	 * @see com.googlecode.horncomb.orm.ibatis.SqlMapEntityDao#queryByExample(java.io.Serializable,
	 *      java.util.Collection)
	 */
	public List<? extends EntityType> queryByExample(EntityType example,
			Order order) throws DataAccessException {
		return this.query(SqlMapEntityDaoUtil
				.convertExampleToConstraints(example), order);
	}

	public Collection<? extends EntityType> queryAll() {
		return this.query(null, null);
	}

	public List<? extends EntityType> queryAll(Order order) {
		return this.query(null, order);
	}

	public PagedList<List<? extends EntityType>> queryAll(Order order,
			int page, int pageSize) {
		return this.query(null, order, page, pageSize);
	}

	@Override
	protected Collection<EntityType> query(ConditionSet constraints)
			throws DataAccessException {
		return super.query(constraints);
	}

	@Override
	protected List<? extends EntityType> query(ConditionSet constraints,
			Order order) throws DataAccessException {
		return super.query(constraints, order);
	}

	@Override
	protected PagedList<List<? extends EntityType>> query(
			ConditionSet constraints, Order order, int page, int pageSize)
			throws DataAccessException {
		return super.query(constraints, order, page, pageSize);
	}

	@Override
	protected PagedList<List<? extends EntityType>> query(
			ConditionSet constraints, Order order, int page, int pageSize,
			String qryStmtName, String queryCountStmtId)
			throws DataAccessException {
		return super.query(constraints, order, page, pageSize, qryStmtName,
				queryCountStmtId);
	}

	@Override
	protected List<EntityType> query(ConditionSet constraints, Order order,
			String qryStmtName) throws DataAccessException {
		return super.query(constraints, order, qryStmtName);
	}

	@Override
	protected CollectionWarpper<Collection<EntityType>> doQuery(
			ConditionSet constraints, Order order, int page, int pageSize,
			String qryStmtName, String qryCntStmtName)
			throws DataAccessException {
		return super.doQuery(constraints, order, page, pageSize, qryStmtName,
				qryCntStmtName);
	}
}
